// $codepro.audit.disable
/**
 * Serializer.java
 */
package edu.gatech.coc.betameisters.utilities;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import biz.source_code.base64Coder.Base64Coder;

public final class Serializer {	
	/**
	 * Turns a serialized object string back into an object
	 * @param s Serialized string form of an object
	 * @return The deserialized object
	 */
    public static Object fromString( String s ) 
    throws IOException, ClassNotFoundException {
    	try {
	        byte[] data = Base64Coder.decode( s );
	        ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( data ) );
	        Object o  = ois.readObject();
	        ois.close(); // $codepro.audit.disable
	        return o; 
    	} catch(Exception e) {
    		return null;
    	}
    }

    /**
	 * Turns an object into its serialized String form
	 * @param o Object to serialize
	 * @return the serialized string object
	 */
    public static String toString(Serializable o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); // $codepro.audit.disable variableShouldBeFinal, closeWhereCreated
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close(); // $codepro.audit.disable closeInFinally, closeWhereCreated
        return new String( Base64Coder.encode( baos.toByteArray() ) );
    }
    
    /**
     * Overrides the toString method
     */
    public String toString(){
    	return "Serialize";
    }
}
